package com.eagle.security.util;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;

public class JsonUtils
{
    private static final ObjectMapper jsonObjectMapper = new ObjectMapper();

    public static String parse(Object value) throws IllegalArgumentException
    {
        if (value == null)
        {
            return null;
        }
        else
        {
            try
            {
                return jsonObjectMapper.writeValueAsString(value);
            }
            catch (Exception e)
            {
                throw new IllegalArgumentException(e);
            }
        }
    }

    public static <T> T convert(String json, Class<T> type) throws IllegalArgumentException
    {

        if (StringUtils.isEmpty(json))
        {
            return null;
        }
        else
        {
            try
            {
                return jsonObjectMapper.readValue(json, type);
            }
            catch (Exception e)
            {
                throw new IllegalArgumentException(e);
            }
        }
    }
}
